<?php

namespace app\api\controller\mycurrency;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\Area;
use app\common\model\Config;
use app\common\model\mycurrency\Picture;
use Psr\Log\NullLogger;
use think\Hook;

/**
 * 其他接口
 */
class Other extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 轮播图
    */
    public function bannerList(){
        $param = $this->request->param();
        $rule = [
            ['position', 'require|in:indexTop', '轮播图位置不能为空|类型错误'],
        ];
        $check = $this->validate($param, $rule);
        if ($check !== true) {
            $this->error($check);
        }
        $where = [
            'status' => Picture::STATUS_XIANSHI,
            'deletetime' => null,
        ];
        switch ($param['position']){
            case 'indexTop'://首页顶部轮播图
                $where['position_id'] = 1;
                break;
            default:
                $this->error('类型参数错误');
                break;
        }
        $data = Picture::where($where)->field('id,title,image')->order('weigh desc,id desc')->select();
        $this->success('',$data);
    }

    /**
     * 发送短信验证码
    */
    public function send(){
        $mobile = $this->request->post("mobile");//手机号
        $event = $this->request->post("event");//发送短信验证码事件 register 手机号注册会员 mobilelogin 手机号登录 changemobile 修改手机号
        $event = $event ? $event : 'bind_mobile';

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }
        $last = Smslib::get($mobile, $event);
        if ($last && time() - $last['createtime'] < 60) {
            $this->error(__('发送频繁'));
        }
        $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
        if ($ipSendTotal >= 5) {
            $this->error(__('发送频繁'));
        }
        if (!Hook::get('sms_send')) {
            $this->error(__('请在后台插件管理安装短信验证插件'));
        }
        $ret = Smslib::send($mobile, null, $event);
        if ($ret) {
            $this->success(__('发送成功'));
        } else {
            $this->error(__('发送失败，请检查短信配置是否正确'));
        }
    }


    /**
     * 获取省市区列表
    */
    public function getArea(){
        $param = $this->request->param();
        $rule = [
            ['pid', 'require', 'pid不能为空'],
        ];
        $check = $this->validate($param, $rule);
        if ($check !== true) {
            $this->error($check);
        }
        $data = Area::where(['pid' => $param['pid']])->field('id,shortname')->select();
        $this->success('',$data);
    }

    /**
     * 获取系统配置
    */
    public function getConfig(){
        $data = Config::where(true)->column('name,value');
        $data['customer_service_qrcode'] = Config::getCustomerServiceQrcode($data['customer_service_qrcode']);
        $this->success('',$data);
    }

}
